﻿/************************************************************************************
 * Copyright (c) 2016 北京大象科技有限公司 All Rights Reserved.
 * CLR版本：4.0.30319.42000
 *公司名称：北京大象科技有限公司
 *命名空间：ET.Framework
 *文件名：  WcfConfigHelper
 *版本号：  V1.0.0.0
 *唯一标识：fc5761a6-7ebe-4223-90bf-2ada86fda3f3
 *创建人：  杜鹏
 *电子邮箱：dupeng@bjdaxiang.cn
 *QQ:       4909004
 *创建时间：2016/3/29 15:39:51
 *描述：    
 *
 *=====================================================================
 *修改标记
 *修改时间：2016/3/29 15:39:51
 *修改人：  杜鹏
 *版本号：  V1.0.0.0
 *描述：
/************************************************************************************/

using System;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.Reflection;

namespace ET.Score.Framework
{
    /// <summary>
    /// 传输参数方式
    /// </summary>
    public enum EnumHttpBindingType
    {
        /// <summary>
        /// 
        /// </summary>
        Small = 0,
        /// <summary>
        /// 
        /// </summary>
        Normal = 1
    }

    /// <summary>
    /// WCF 配置的帮助类
    /// </summary>
    public static class WcfConfigHelper
    {
        /// <summary>
        /// 获取bassHTTP绑定
        /// </summary>
        /// <returns></returns>
        public static BasicHttpBinding GetBasicHttpBinding(EnumHttpBindingType bindingType)
        {
            BasicHttpBinding ws = new BasicHttpBinding(BasicHttpSecurityMode.None);
            ws.CloseTimeout = new TimeSpan(0, 5, 0);
            ws.OpenTimeout = new TimeSpan(0, 5, 0);
            ws.ReceiveTimeout = new TimeSpan(0, 20, 0);
            ws.SendTimeout = new TimeSpan(0, 5, 0);
            ws.AllowCookies = false;
            ws.BypassProxyOnLocal = false;
            ws.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            ws.MaxBufferSize = 2147483647;
            ws.MaxBufferPoolSize = 2147483647;
            ws.MaxReceivedMessageSize = 2147483647;
            ws.MessageEncoding = WSMessageEncoding.Text;
            ws.TextEncoding = Encoding.UTF8;
            ws.TransferMode = TransferMode.Buffered;
            ws.UseDefaultWebProxy = true;
            ws.AllowCookies = false;

            ws.ReaderQuotas.MaxDepth = 2147483647;
            ws.ReaderQuotas.MaxStringContentLength = 2147483647;
            ws.ReaderQuotas.MaxArrayLength = 2147483647;
            ws.ReaderQuotas.MaxBytesPerRead = 2147483647;
            ws.ReaderQuotas.MaxNameTableCharCount = 2147483647;

            ws.Security.Mode = BasicHttpSecurityMode.None;
            ws.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            ws.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            ws.Security.Transport.Realm = "";
            ws.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            ws.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            return ws;
        }

        /// <summary>
        /// 获取默认HTTP绑定
        /// </summary>
        /// <returns></returns>
        public static WSHttpBinding GetWsHttpBinding(EnumHttpBindingType bindingType)
        {
            WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
            ws.CloseTimeout = new TimeSpan(0, 5, 0);
            ws.OpenTimeout = new TimeSpan(0, 5, 0);
            ws.ReceiveTimeout = new TimeSpan(0, 20, 0);
            ws.SendTimeout = new TimeSpan(0, 5, 0);
            ws.BypassProxyOnLocal = false;
            ws.TransactionFlow = false;
            ws.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            ws.MaxBufferPoolSize = 2147483647;// 524288;
            ws.MaxReceivedMessageSize = 2147483647;
            ws.MessageEncoding = WSMessageEncoding.Text;
            ws.TextEncoding = Encoding.UTF8;
            ws.UseDefaultWebProxy = true;
            ws.AllowCookies = false;

            ws.ReaderQuotas.MaxDepth = 2147483647;
            ws.ReaderQuotas.MaxStringContentLength = 2147483647;
            ws.ReaderQuotas.MaxArrayLength = 2147483647;
            ws.ReaderQuotas.MaxBytesPerRead = 2147483647;
            ws.ReaderQuotas.MaxNameTableCharCount = 2147483647;

            ws.ReliableSession.Ordered = true;
            ws.ReliableSession.InactivityTimeout = new TimeSpan(23, 59, 0);
            ws.ReliableSession.Enabled = true;
            ws.Security.Mode = SecurityMode.None;
            ws.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            ws.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            ws.Security.Transport.Realm = "";
            ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            ws.Security.Message.NegotiateServiceCredential = true;
            ws.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            ws.Security.Message.EstablishSecurityContext = false;

            return ws;
        }

        /// <summary>
        /// 获取默认HTTP绑定（双工通信）
        /// </summary>
        /// <returns></returns>
        public static WSDualHttpBinding GetWsDualHttpBinding(EnumHttpBindingType bindingType)
        {
            WSDualHttpBinding ws = new WSDualHttpBinding(WSDualHttpSecurityMode.Message);
            ws.CloseTimeout = new TimeSpan(0, 5, 0);
            ws.OpenTimeout = new TimeSpan(0, 5, 0);
            ws.ReceiveTimeout = new TimeSpan(0, 20, 0);
            ws.SendTimeout = new TimeSpan(0, 5, 0);
            ws.BypassProxyOnLocal = false;
            ws.TransactionFlow = false;
            ws.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            ws.MaxBufferPoolSize = 2147483647;// 524288;
            ws.MaxReceivedMessageSize = 2147483647;
            ws.MessageEncoding = WSMessageEncoding.Text;
            ws.TextEncoding = Encoding.UTF8;
            ws.UseDefaultWebProxy = true;

            ws.ReaderQuotas.MaxDepth = 2147483647;
            ws.ReaderQuotas.MaxStringContentLength = 2147483647;
            ws.ReaderQuotas.MaxArrayLength = 2147483647;
            ws.ReaderQuotas.MaxBytesPerRead = 2147483647;
            ws.ReaderQuotas.MaxNameTableCharCount = 2147483647;

            ws.ReliableSession.Ordered = true;
            ws.ReliableSession.InactivityTimeout = new TimeSpan(23, 59, 0);

            ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            ws.Security.Message.NegotiateServiceCredential = true;
            ws.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

            return ws;
        }

        /// <summary>
        /// 获取默认TCP绑定
        /// </summary>
        /// <returns></returns>
        public static NetTcpBinding GetNetTcpBinding()
        {
            NetTcpBinding nettcp = new NetTcpBinding(SecurityMode.None);
            nettcp.CloseTimeout = new TimeSpan(0, 5, 0);
            nettcp.OpenTimeout = new TimeSpan(0, 5, 0);
            nettcp.ReceiveTimeout = new TimeSpan(0, 20, 0);
            nettcp.SendTimeout = new TimeSpan(0, 5, 0);

            nettcp.TransactionFlow = false;
            nettcp.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            nettcp.MaxBufferPoolSize = 1073741824;
            nettcp.MaxBufferSize = 1073741824;
            nettcp.MaxReceivedMessageSize = 1073741824;

            nettcp.ReaderQuotas.MaxDepth = 6553600;
            nettcp.ReaderQuotas.MaxStringContentLength = 2147483647;
            nettcp.ReaderQuotas.MaxArrayLength = 6553600;
            nettcp.ReaderQuotas.MaxBytesPerRead = 6553600;
            nettcp.ReaderQuotas.MaxNameTableCharCount = 6553600;

            nettcp.ReliableSession.Ordered = true;
            nettcp.ReliableSession.InactivityTimeout = new TimeSpan(23, 59, 0);
            nettcp.ReliableSession.Enabled = false;

            nettcp.Security.Mode = SecurityMode.None;
            nettcp.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            nettcp.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

            return nettcp;
        }

        /// <summary>
        /// 获取默认TCP绑定（双工通信）
        /// </summary>
        /// <returns></returns>
        public static NetTcpBinding GetDuplexBinding()
        {
            NetTcpBinding nettcp = new NetTcpBinding(SecurityMode.None);
            nettcp.CloseTimeout = new TimeSpan(0, 5, 0);
            nettcp.OpenTimeout = new TimeSpan(0, 5, 0);
            nettcp.ReceiveTimeout = new TimeSpan(0, 20, 0);
            nettcp.SendTimeout = new TimeSpan(0, 5, 0);

            nettcp.TransactionFlow = false;
            nettcp.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            nettcp.MaxBufferPoolSize = 1073741824;
            nettcp.MaxBufferSize = 1073741824;
            nettcp.MaxReceivedMessageSize = 1073741824;

            nettcp.ReliableSession.Ordered = true;
            nettcp.ReliableSession.InactivityTimeout = new TimeSpan(23, 59, 0);
            nettcp.ReliableSession.Enabled = false;

            nettcp.Security.Mode = SecurityMode.None;
            nettcp.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            nettcp.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;

            return nettcp;
        }

        /// <summary>
        /// 获取默认命名管道绑定
        /// </summary>
        /// <returns></returns>
        public static NetNamedPipeBinding GetNetPipeBinding()
        {
            //NetNamedPipeBinding
            NetNamedPipeBinding netnamepipe = new NetNamedPipeBinding();
            netnamepipe.CloseTimeout = new TimeSpan(0, 5, 0);
            netnamepipe.OpenTimeout = new TimeSpan(0, 5, 0);
            netnamepipe.ReceiveTimeout = new TimeSpan(0, 20, 0);
            netnamepipe.SendTimeout = new TimeSpan(0, 5, 0);

            netnamepipe.TransactionFlow = false;
            netnamepipe.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            netnamepipe.MaxBufferPoolSize = 1073741824;
            netnamepipe.MaxBufferSize = 1073741824;
            netnamepipe.MaxReceivedMessageSize = 1073741824;

            return netnamepipe;
        }

        /// <summary>
        /// 内部元数据行为
        /// 配置文件：<serviceMetadata httpGetEnabled="true" />
        /// </summary>
        /// <returns></returns>
        public static ServiceMetadataBehavior GetWsMetaBehavior()
        {
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            // 为避免泄漏元数据信息，请在部署前将以下值设置为 false 并删除上面的元数据终结点
            behavior.HttpGetEnabled = true;

            // behavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            return behavior;
        }

        /// <summary>
        /// 内部调试行为
        /// 配置文件：<serviceDebug includeExceptionDetailInFaults="true" />
        /// </summary>
        /// <returns></returns>
        public static ServiceDebugBehavior GetserviceDebugBehavior()
        {
            ServiceDebugBehavior behavior = new ServiceDebugBehavior();
            // 要接收故障异常详细信息以进行调试，请将下值设置为 true。在部署前设置为 false 以避免泄漏异常信息
            behavior.IncludeExceptionDetailInFaults = true;

            return behavior;
        }

        /// <summary>
        /// 内部流作用行为
        /// 配置文件：<serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions ="100" maxConcurrentInstances ="100"/>
        /// </summary>
        /// <returns></returns>
        public static ServiceThrottlingBehavior GetserviceThrottlingBehavior()
        {
            ServiceThrottlingBehavior behavior = new ServiceThrottlingBehavior();
            behavior.MaxConcurrentCalls = 200;
            behavior.MaxConcurrentSessions = 1000;
            behavior.MaxConcurrentInstances = 50000;

            return behavior;
        }

        /// <summary>
        /// 内部系列化参数设置
        /// 配置文件：<dataContractSerializer maxItemsInObjectGraph="2147483647" />
        /// </summary>
        /// <returns></returns>
        public static void SetdataContractSerializer(ServiceHost hostTemp)
        {
            Type t = hostTemp.GetType();

            object obj = t.Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior",
                true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { false, Int32.MaxValue }, null, null);

            IServiceBehavior myServiceBehavior = obj as IServiceBehavior;
            if (myServiceBehavior != null)
            {
                hostTemp.Description.Behaviors.Add(myServiceBehavior);
            }
        }
    }
}
